package leetcode;

public class Code43 {

    public static void main(String[] args) {
        System.out.println(new Code43().multiply("9133","0"));
    }

    public String multiply(String num1, String num2) {
        return bigMmltiplication(num1, num2);
    }

    private String bigMmltiplication(String num1, String num2) {
        StringBuilder beforeSum = new StringBuilder();
        beforeSum.append('0');
        for (int index = num2.length() - 1; index >= 0; index--) {
            StringBuilder nowMUl = getMulItem(num1, num2.charAt(index));
            for (int addindex = index; addindex < num2.length() - 1; addindex++) {
                nowMUl.append('0');
            }
            beforeSum = getSum(beforeSum, nowMUl);
        }
        int a=0;
        for (;a<beforeSum.length();a++){
            if(beforeSum.charAt(a)!='0'){
                break;
            }
        }
        beforeSum.delete(0,a);
        if(beforeSum.length()==0){
            beforeSum.append("0");
        }
        return beforeSum.toString();
    }



    private StringBuilder getSum(StringBuilder before, StringBuilder other) {
        if (before.length() < other.length()) {
            StringBuilder i = before;
            before = other;
            other = i;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int sumBefore = 0;
        int a = before.length() - 1, b = other.length() - 1;
        while (b >= 0 || sumBefore != 0) {
            int otherNumber = 0;
            int beforeNumber= 0;
            if (b >= 0) {
                otherNumber = Character.getNumericValue(other.charAt(b));
            }
            if(a>=0){
                beforeNumber = Character.getNumericValue(before.charAt(a));
            }
            int now = beforeNumber + otherNumber + sumBefore;
            stringBuilder.append(String.valueOf(now % 10));
            sumBefore = now / 10;
            if (b >= 0) {
                b--;
            }
            if(a>=0){
                a--;
            }
        }
        String aaa=before.reverse().toString().substring(before.length()-1-a,before.length());
        stringBuilder.append(aaa);
        return stringBuilder.reverse();
    }

    //一位乘法
    private StringBuilder getMulItem(String num1, char item) {
        StringBuilder stringBuilder = new StringBuilder();
        int[] jin = new int[2];
        int before = 0;
        int it = Character.getNumericValue(item);
        for (int a = num1.length() - 1; a >= 0; a--) {
            mul(Character.getNumericValue(num1.charAt(a)), it, jin);
            int nowEnd = before + jin[1];
            stringBuilder.append(String.valueOf(nowEnd % 10));
            before = jin[0] + nowEnd / 10;
        }
        if(before!=0){
            stringBuilder.append(String.valueOf(before));
        }
        return stringBuilder.reverse();
    }

    private int[] mul(int a, int b, int[] jin) {
        int su = a * b;
        jin[0] = su / 10;
        jin[1] = su % 10;
        return jin;
    }
}
